<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>02_组件的setState流程</title>
</head>

<body>
    <div id="app"></div>

    <script src="../../00_课程资料/js/react.development.js"></script>
    <script src="../../00_课程资料/js/react-dom.development.js"></script>
    <script src="../../00_课程资料/js/babel.min.js"></script>

    <script type="text/babel">
        class Count extends React.Component {
            // 组件最先调用constructor
            constructor(props){
                super(props)
                console.log('Count - constructor');
            }

            state = {
                count: 0
            }

            add = () => {
                const { count } = this.state
                this.setState({ count: count + 1 })
            }

            // 组件将要挂载触发
            componentWillMount(){
                console.log('Count - componentWillMount');
            }

            // 组件挂载时
            render() {
                console.log('Count - render');
                return (
                    <div>
                        <h2>当前的和为：{this.state.count}</h2>
                        <button onClick={this.add}>点我+1</button>
                    </div>
                )
            }

            // 组件挂载完成后触发
            componentDidMount(){
                console.log('Count - componentDidMount');
            }

            // 组件是否更新的阀门，这个方法返回一个布尔值，如果不定义，则默认返回true
            shouldComponentUpdate(){
                return true
                // 如果返回false则页面将不能更新
            }
            
            // 组件将要更新
            componentWillUpdate(){
                console.log('Count - componentWillUpdate');
            }

            // 组件更新完成
            componentDidUpdate(){
                console.log('Count - componentDidUpdate');
            }
        }
        ReactDOM.render(<Count />, document.getElementById("app"))
    </script>
</body>

</html>